﻿using System;
using System.Collections.Generic;
using System.IO;

namespace DevTools.BinDeleter.Components
{
    public class Deleter
    {
        #region singleton
        private static Deleter _instance;

        public static Deleter Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new Deleter();
                return _instance;
            }
        }

        private Deleter()
        {
        }
        #endregion

        public void Delete(ICollection<FolderInfo> folders)
        {
            foreach (FolderInfo folder in folders)
            {
                try
                {
                    Directory.Delete(folder.Directory.FullName, true);
                    folder.Deleted = true;
                }
                catch (Exception ex)
                {
                    folder.ErrorMessage = ex.Message;
                }
            }

            OnDeleted();
        }

        public ICollection<FolderInfo> GetBinFolders(string rootPath)
        {
            ICollection<FolderInfo> folders = new List<FolderInfo>();
            DirectoryInfo root = new DirectoryInfo(rootPath);

            GetBinFoldersCore(root, folders);

            OnFoldersLoaded(folders);

            return folders;
        }

        private static void GetBinFoldersCore(DirectoryInfo directory, ICollection<FolderInfo> folders)
        {
            DirectoryInfo[] directories = directory.GetDirectories();

            foreach (DirectoryInfo directoryInfo in directories)
            {
                if (directoryInfo.Name == "bin" || directoryInfo.Name == "obj")
                    folders.Add(new FolderInfo(directoryInfo));

                GetBinFoldersCore(directoryInfo, folders);
            }
        }

        #region Deleted event things
        public event EventHandler<EventArgs> Deleted;

        protected virtual void OnDeleted(EventArgs e)
        {
            if (Deleted != null)
                Deleted(this, e);
        }

        protected virtual void OnDeleted()
        {
            if (Deleted != null)
                Deleted(this, new EventArgs());
        }
        #endregion

        #region FoldersLoaded event things
        public event EventHandler<FoldersLoadedEventArgs> FoldersLoaded;

        protected virtual void OnFoldersLoaded(FoldersLoadedEventArgs e)
        {
            if (FoldersLoaded != null)
                FoldersLoaded(this, e);
        }

        protected virtual void OnFoldersLoaded(ICollection<FolderInfo> folders)
        {
            if (FoldersLoaded != null)
                FoldersLoaded(this, new FoldersLoadedEventArgs(folders));
        }

        public class FoldersLoadedEventArgs : EventArgs
        {
            public ICollection<FolderInfo> Folders { get; set; }

            public FoldersLoadedEventArgs(ICollection<FolderInfo> folders)
            {
                Folders = folders;
            }
        }
        #endregion
    }
}